# -*- coding: utf-8 -*-

import unittest
import os.path

from kivy.lang import Builder
from kivy.core.window import Window

from presenter import Presenter
from model.gamestate import *
from ui.welcome_screen import WelcomeScreen
from main import MainView
from ui.quick_game_settings import QuickGameSettings
from ui.from_to import FromToWidget


class PresenterTest(unittest.TestCase):
    def setUp(self):
        self.presenter = Presenter(None, './data/config.ini')
        players = Players([Player('Andrey', []), Player('Boris', []),
                           Player('Charlie', []), Player('Dmitry', [])])
        players.set_next_pair_strategy(TraditionalStrategy(players.players))
        hat = Hat(['стул', 'шапка', 'кастрюлька'])
        game_round = GameRound(20, [], '', (players.players[0],
                                            players.players[2]))

        self.presenter.game_state = GameState(players, hat,
                                              game_round, 0, Phase.WELCOME,
                                              None, Settings(20, 30))
        self.presenter.main_view = MainView()
        self.game_state_before_pause = self.presenter.game_state

    def test_pause_and_resume_app(self):
        self.presenter.register_view(Phase.WELCOME,
                                     WelcomeScreen,
                                     [])
        self.presenter.close_app()
        self.presenter.resume_app()
        print self.presenter.game_state
        print self.game_state_before_pause
        self.assertEqual(self.presenter.game_state,
                         self.game_state_before_pause)

    def test_resuming_game_screen(self):
        self.presenter.register_view(Phase.QUICK_GAME_SETTINGS,
                                     QuickGameSettings,
                                     [])
        self.presenter.register_view(Phase.FROM_TO, FromToWidget, [])
        self.presenter.game_state.phase = Phase.GUESS_WORD
        self.presenter.close_app()
        self.presenter.resume_app()
        self.assertEquals(self.presenter.game_state.phase, Phase.FROM_TO)

    def test_rotation(self):
        for phase_name in range(16):
            self.presenter.game_state.phase = phase_name
            self.presenter.rotate()
            if phase_name in [Phase.FROM_TO, Phase.COUNTDOWN,
                              Phase.GUESS_WORD, Phase.RESULTS_OF_ONE_PAIR]:
                self.assertEquals(
                    Window.rotation,
                    self.presenter.settings.graphics.horizontal_orientation)


if __name__ == "__main__":
    Builder.load_file(os.path.join(os.path.dirname(__file__), '..',
                                   'mainview.kv'))
    unittest.testmod()
